local skel = fk.CreateSkill {
  name = "emo__pokou",
  dynamic_desc = function (self, player, lang)
    local pid = Fk:currentRoom():getBanner("emo__pokou_last")
    if pid then
      local winner = Fk:currentRoom():getPlayerById(pid)
      if winner then
        return "emo__pokou_inner:" .. Fk:translate(winner.general, lang)
      end
    end
  end,
}

Fk:loadTranslationTable{
  ["emo__pokou"] = "迫寇",
  [":emo__pokou"] = "出牌阶段限两次，你可以发起拼点，唯一没赢的角色选择一项：1.翻面；2.弃置X张牌；3.重复此拼点。结束阶段，最后一次因此赢的角色摸X张牌（X为本回合拼点次数）。",

  [":emo__pokou_inner"] = "出牌阶段限两次，你可以发起拼点，唯一没赢的角色选择一项：1.翻面；2.弃置X张牌；3.重复此拼点。结束阶段，最后一次因此赢的角色（{1}）摸X张牌（X为本回合拼点次数）。",


  ["#emo__pokou"] = "迫寇：你可以拼点，没赢的一方选择翻面、弃牌或再拼点",
  ["emo__pokou_turnover"] = "翻面",
  ["emo__pokou_discard"] = "弃置%arg张牌",
  ["emo__pokou_repeat"] = "重复此拼点",
  ["#emo__pokou-choice"] = "迫寇：你须选择一项执行",

  ["$emo__pokou1"] = "此等伎俩，如何能瞒过我！",
  ["$emo__pokou2"] = "汝之计略，吾早已勘破！",
}

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#emo__pokou",
  card_num = 0,
  target_num = 1,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and player:canPindian(to_select)
  end,
  times = function (self, player)
    return 2 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) < 2 and not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    while player:canPindian(to) do
      local pd = player:pindian({to}, skel.name)
      if not (pd and pd.results and pd.results[to]) then return end
      local winner = pd.results[to].winner
      if not winner then return end
      room:setBanner("emo__pokou_last", winner.id)
      local loser = winner == player and to or player
      if loser.dead then return end
      local pdNum = #room.logic:getEventsOfScope(GameEvent.Pindian, 999, Util.TrueFunc, Player.HistoryTurn)
      local all_choices = {
        "emo__pokou_turnover",
        "emo__pokou_discard:::"..pdNum,
        "emo__pokou_repeat",
      }
      local choices = table.simpleClone(all_choices)
      if player.dead or to.dead or not player:canPindian(to) then table.remove(choices, 3) end
      local canDiscardNum = #table.filter(loser:getCardIds("he"), function (id)
        return not loser:prohibitDiscard(id)
      end)
      if canDiscardNum < pdNum then table.remove(choices, 2) end
      local choice = room:askToChoice(loser, {
        skill_name = skel.name, choices = choices, all_choices = all_choices, prompt = "#emo__pokou-choice",
      })
      if choice == "emo__pokou_repeat" then
        player:broadcastSkillInvoke(skel.name)
      else
        if choice == "emo__pokou_turnover" then
          loser:turnOver()
        else
          room:askToDiscard(loser, {min_num = pdNum, max_num = pdNum, include_equip = true, skill_name = skel.name, cancelable = false})
        end
        break
      end
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player == target and player.phase == Player.Finish then
      local pid = player.room:getBanner("emo__pokou_last")
      if not pid then return false end
      local to = player.room:getPlayerById(pid)
      return to and to:isAlive()
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local pdNum = #room.logic:getEventsOfScope(GameEvent.Pindian, 999, Util.TrueFunc, Player.HistoryTurn)
    local to = room:getPlayerById(room:getBanner("emo__pokou_last"))
    if to then
      to:drawCards(pdNum, skel.name)
    end
  end,
})

return skel
